/**
 * @(#)AddReserveTaskTimer.java 2017年09月26日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.timer;

import java.util.List;

import javax.annotation.Resource;

import org.jsbd.boss.domian.channel.QueryUserClientVo;
import org.jsbd.boss.domian.channel.UserClientInfo;
import org.jsbd.boss.enums.CarrierEnum;
import org.jsbd.boss.service.ClientManager;
import org.jsbd.boss.service.IUserClientInfoService;
import org.jsbd.boss.util.CacheKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.hziee.common.queue.DelayExecuteBuffer;

/**
 * 
 * 历史用户imei校验
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Service
public class HistoryClientImeiCheckTaskTimer extends TaskAdapterHandler {
	public static final Logger LOGGER = LoggerFactory.getLogger(HistoryClientImeiCheckTaskTimer.class);

	@Resource
	private IUserClientInfoService userClientService;

	@Resource
	private ClientManager clientManager;

	@Resource
	private DelayExecuteBuffer<UserClientInfo> userClientBatchUpdateBuffer;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.timer.TaskAdapterHandler#init()
	 */
	@Override
	public boolean init() {
		Object flag = cacheParamManager.getParamValue(CacheKey.HISTORY_CLIENT_CHECK_FLAG);
		if (flag != null && "1".equals(flag)) {
			return true;
		} else {
			return false;
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jsbd.boss.timer.TaskHandler#execute()
	 */
	@Override
	public void run() {
		if (init()) {
			CarrierEnum[] carriers = new CarrierEnum[] { CarrierEnum.MOBILE, CarrierEnum.TELECOM, CarrierEnum.UNICOM };
			int length = 100;
			Object lengthObj = cacheParamManager.getParamValue(CacheKey.HISTORY_CLIENT_CHECK_LENGTH);
			if (lengthObj != null) {
				length = Integer.parseInt(lengthObj.toString());
			}
			for (int i = 0; i < carriers.length; i++) {
				for (int j = 0; j < 31; j++) {
					String lockKey = CacheKey.HISTORY_CLIENT_CHECK_LOCK + i + j;
					if (redisClient.evalSetNx(lockKey, 10)) {
						Object idObj = redisClient.getHash(CacheKey.HISTORY_CLIENT_CHECK_INDEX, i + "_" + j);
						Long id = 0L;
						if (idObj != null) {
							id = Long.parseLong(idObj.toString());
						}
						QueryUserClientVo userVo = new QueryUserClientVo();
						userVo.setCarrier(carriers[i].getType());
						userVo.setId(id);
						userVo.setLength(length);
						userVo.setProvince(j);
						List<UserClientInfo> userList = userClientService.selectImsiImeiList(userVo);
						if (userList != null && !userList.isEmpty()) {
							LOGGER.warn("历史用户 {} {} {} {}", new Object[] { carriers[i].getName(), j, id, userList.size() });
							id = userList.get(userList.size() - 1).getId();
							redisClient.putHash(CacheKey.HISTORY_CLIENT_CHECK_INDEX, i + "_" + j, id.toString());
							for (UserClientInfo client : userList) {
								String imsi = client.getImsi();
								String imei = client.getImei();
								client.setCarrier(carriers[i].getType());
								client.setProvince(j);
								imei = clientManager.checkAndGetImsiAndImei(imsi, imei);
								if (imei != null) {
									client.setImei(imei);
									userClientBatchUpdateBuffer.add(client);
								}
							}
						}
					}
				}
			}
		}
	}
}
